﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Configuration;
using System.Web.Management;
using System.Web.Security;
using System.Threading;
using System.Data.Linq;
using System.Resources;

namespace SurveySystem.Core
{
    public static class ManagementBLL
    {
        private static object _monitor = new object();
        public static void InitializeBLL()
        {
            lock (_monitor)
            {
                Debug.Print("Inititalizing Business Logic Layer ...");

                new EmailPollingAgent().Start();
                new ExpiryPollingAgent().Start();
                new SMSPollingAgent().Start();

                SurveysBLL.SurveyPublished -= CommunicationUtils.HandleEmailsOnNewSurvey;
                SurveysBLL.SurveyPublished += CommunicationUtils.HandleEmailsOnNewSurvey;
                SurveysBLL.SurveyPublished -= CommunicationUtils.HandleSMSOnNewSurvey;
                SurveysBLL.SurveyPublished += CommunicationUtils.HandleSMSOnNewSurvey;

                SurveysBLL.SurveyExpired -= CommunicationUtils.HandleEmailsOnExpiredSurvey;
                SurveysBLL.SurveyExpired += CommunicationUtils.HandleEmailsOnExpiredSurvey;
                SurveysBLL.SurveyExpired -= CommunicationUtils.HandleSMSOnExpiredSurvey;
                SurveysBLL.SurveyExpired += CommunicationUtils.HandleSMSOnExpiredSurvey;

                SurveySystemDataContext dc = CommonUtils.GetDc();
                if (!dc.DatabaseExists())
                {
                    CommonUtils.CreateDb(dc);
                }

            }
        }
        
        public static ResourceManager MessageResources
        {
            get { return CommonUtils.MessageResources; }
            set { CommonUtils.MessageResources = value; }

        }

        public static string Ping()
        {
            return "Pong!";
        }

        public static void ResetDb()
        {
            CommonUtils.ResetDB();
        }

        
    }
}
